//
//  DSOrderListController.m
//  DSJY
//
//  Created by 冯华强 on 16/9/24.
//  Copyright © 2016年 dsjy. All rights reserved.
//  订单列表

#import "DSOrderListController.h"
#import "DSOrderListCell.h"
#import "MTConst.h"
#import "DSOrderListViewModel.h"
#import "DSLearnStatis.h"
#import "UIImage+Extension.h"
#import "DSAccountViewModel.h"

static NSString *identifier = @"OrderListCell";
@interface DSOrderListController () <UITableViewDelegate, UITableViewDataSource>
#pragma mark - 拖线属性
@property (weak, nonatomic) IBOutlet UITableView *tableView;
//属性
@property (nonatomic, strong) DSOrderListViewModel *orderListVM;
@end

@implementation DSOrderListController

#pragma mark - 懒加载
- (DSOrderListViewModel *)orderListVM
{
    if (_orderListVM == nil) {
        _orderListVM = [[DSOrderListViewModel alloc] init];
    }
    return _orderListVM;
}

#pragma mark - 系统回调
- (void)viewDidLoad {
    [super viewDidLoad];
    //显示backItem
    [self setupnavigationItem];
    //绑定VM
    [self bindViewModel];
    //绑定事件
    [self bindEvent];
}

#pragma mark - 设置UI
- (void)setupnavigationItem
{
    self.showBackItem = YES;
}

- (void)bindViewModel
{
    @weakify(self);
    self.tableView.allowsSelection = NO;
    //加载上拉下拉
    self.tableView.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        @strongify(self);
        [self checkNetWorkWithRefreshType:YQRefreshTypeHeader];
    }];
}

#pragma mark - 设置事件
- (void)bindEvent
{
    @weakify(self);
    //如果验证通过，调用命令信号
    [self.orderListVM.requestCommand.executionSignals.switchToLatest subscribeNext:^(id x) {
        @strongify(self);
        [self reloadDataWithTableView:self.tableView currentPage:self.orderListVM.pageNum totalPage:self.orderListVM.response.total_page];
    }];
    
    //有网络
    [self.networkWiFiWWANSignal subscribeNext:^(RACTuple *tuple) {
        @strongify(self);
        // 执行请求
        [self.orderListVM.requestCommand execute:tuple[0]];
    }];
    
    //无网络
    [self.networkUnknownNotReachableSignal subscribeNext:^(id x) {
        @strongify(self);
        [self showEmptyHUDWithView:self.tableView];
    }];
    
    //执行请求
    [self checkNetWorkWithRefreshType:YQRefreshTypeHeader];
}

#pragma mark - <UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.orderListVM.orderList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DSOrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    cell.order = self.orderListVM.orderList[indexPath.row];
    return cell;
}

#pragma mark - <UITableViewDelegate>
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return XCYTableHead0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return XCYTableHead0;
}

@end
